package com.example.newdemo.view.recyclerView.demo6;

import android.os.Bundle;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.example.newdemo.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;


/**
 * RecyclerView实现瀑布流
 *
 * 由于该例子比较简单，因此顺便介绍万能Adapter方案
 */
public class RecyclerViewActivity6 extends AppCompatActivity {
    private RecyclerView mRv;
    private QuickAdapter<Integer> mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_recycler_view6);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        mRv = (RecyclerView) findViewById(R.id.rv);
        mRv.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
        mAdapter = new QuickAdapter<Integer>(initData()) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_recycler_view6;
            }

            @Override
            public void convert(VH holder, Integer data, int position) {
                ImageView imageView = holder.getView(R.id.image);
                Picasso.with(RecyclerViewActivity6.this).load(data).into(imageView);
                //holder.itemView.setOnClickListener();  此处添加点击事件
            }

            @Override
            public int getItemViewType(int position) {
                return super.getItemViewType(position);
            }
        };
        mAdapter.setHasStableIds(true);
        ((SimpleItemAnimator)mRv.getItemAnimator()).setSupportsChangeAnimations(false);
        mRv.setAdapter(mAdapter);

    }

    public List<Integer> initData(){
        Integer[] images = {R.drawable.s1, R.drawable.s2, R.drawable.s3, R.drawable.s4, R.drawable.s5,
                        R.drawable.s6, R.drawable.s7, R.drawable.s8, R.drawable.s9, R.drawable.s10
                };
        ArrayList<Integer> list = new ArrayList<>();
        for(int i=0;i<2;i++){
            for(Integer image:images){
                list.add(image);
            }
        }
        return list;
    }


    private void getAdapter(){
//        mAdapter = new QuickAdapter<ColorSpace.Model>(data) {
//            @Override
//            public int getLayoutId(int viewType) {
//                switch(viewType){
//                    case TYPE_1:
//                        return R.layout.item_1;
//                    case TYPE_2:
//                        return R.layout.item_2;
//                }
//            }
//
//            @Override
//            public int getItemViewType(int position) {
//                if(position % 2 == 0){
//                    return TYPE_1;
//                } else{
//                    return TYPE_2;
//                }
//            }
//
//            @Override
//            public void convert(VH holder, Model data, int position) {
//                int type = getItemViewType(position);
//                switch(type){
//                    case TYPE_1:
//                        holder.setText(R.id.text, data.text);
//                        break;
//                    case TYPE_2:
//                        holder.setImage(R.id.image, data.image);
//                        break;
//                }
//            }
//        };
    }




}
